package com.zb.system.controller;


import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.exception.ZbException;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjRoleService;
import com.zb.utils.TokenUtil;
import com.zb.vo.RoleAuthorVo;
import com.zb.vo.RoleSelectVo;
import com.zb.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2023-07-18
 */
@Api(tags = "角色类接口")
@RestController
@RequestMapping("/system/role")
public class ZjRoleController {
    @Resource
    private ZjRoleService roleService;

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, remark = "角色 - 新增或修改角色权限")
    @ApiOperation("新增或修改角色权限")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid RoleVo roleVo) throws ZbException {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        roleService.save(custAccId, roleVo);
        return Result.success(null == roleVo.getId()? "新增角色成功" : "更改角色成功");
    }

    @ApiOperation("获取角色列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody RoleSelectVo roleSelectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        roleSelectVo.setCustAccId(custAccId);
        return Result.success("获取角色列表成功").data(roleService.getList(roleSelectVo));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.UPDATE, remark = "角色 - 批量启用或禁用角色状态")
    @ApiOperation("批量启用或禁用角色状态")
    @PutMapping("/disable")
    public Result disable(@RequestParam @ApiParam(value = "是否禁用：false-正常，true-禁用", required = true, example = "disable") boolean status,
                          @RequestBody @ApiParam(value = "角色ID集合", required = true) List<Long> idList) {
        roleService.updateDisableBatchByIds(idList, status);
        return Result.success(status? "禁用角色成功" : "启用角色成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.SYTEM_MODULE_TYPE, type = OperationLogType.DELETE, remark = "角色 - 批量删除角色")
    @ApiOperation("批量删除角色")
    @DeleteMapping("/")
    public Result remove(@RequestBody @ApiParam(value = "角色ID集合", required = true) List<Long> idList) {
        roleService.existAdminByIds(idList);
        roleService.removeByIds(idList);
        return Result.success("角色删除成功");
    }

    @ApiOperation("保存角色权限")
    @PostMapping("/saveRoleAuthor/{id}")
    public Result saveRoleAuthor(HttpServletRequest request, @PathVariable @ApiParam(value = "角色ID", required = true) Long id,
                                 @RequestBody List<RoleAuthorVo> roleAuthorVos) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        roleService.saveRoleAuthor(custAccId, id, roleAuthorVos);
        return Result.success("分配权限成功");
    }

    @ApiOperation("根据角色ID获取角色权限")
    @GetMapping("/getRoleAuthor/{id}")
    public Result getRoleAuthor(HttpServletRequest request, @PathVariable @ApiParam(value = "角色ID", required = true) Long id) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取角色权限成功").data(PageUtil.rows(roleService.getRoleAuthor(custAccId, id)));
    }

    // 根据账号获取对应权限
    @ApiOperation("根据账号获取菜单权限")
    @GetMapping("/getAuthors")
    public Result getAuthors(HttpServletRequest request) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        return Result.success("获取菜单权限成功").data(PageUtil.rows(roleService.getAuthors(loginInfo)));
    }



}
